package com.liu.study.oauth2.server.basic.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 自定义用户详情服务
 *
 * @author lwa
 * @since 2025/4/25
 */
@Component("myUserDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    private static Logger logger = LoggerFactory.getLogger(MyUserDetailsService.class);

    /**
     * 根据用户名 --> 查找用户信息。
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("自定义【UserDetails服务】 --> 可以根据用户username获取用户信息 --> 用户的username为；{}", username);

		// 01、获取数据库中的密码、权限
        String dbPassword = "123456";
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String dbEncodedPassword = passwordEncoder.encode(dbPassword);

        return new User(username, "{bcrypt}" + dbEncodedPassword, AuthorityUtils.commaSeparatedStringToAuthorityList("message.read"));
    }
}
